import Instance from './Instance';

export interface IStringUtils {
    trimSpace(str: string): string; //去除所有空格
    isChinese(str: string): boolean; //是否为中文
    getLength(str: string): number; //获取字符串长度 中文为2
}

export class StringUtils implements IStringUtils {
    public static getInstance(): StringUtils {
        return Instance.get<StringUtils>(StringUtils);
    }

    trimSpace(str: string): string {
        return str.replace(/\s+/g, '');
    }

    isChinese(str: string): boolean {
        return /^[\u4e00-\u9fa5]+$/.test(str);
    }

    getLength(str: string): number {
        let len = 0;
        str.split('').forEach((v) => {
            len += this.isChinese(v) ? 2 : 1;
        });
        return len;
    }
}
